package com.zhuhjay.common.netty.util;

import com.corundumstudio.socketio.SocketIOClient;
import com.corundumstudio.socketio.SocketIOServer;
import com.zhuhjay.common.base.util.SpringContextHolder;

/**
 * NettySocket工具类
 * @author ZhuHJay
 * @date 2023/11/13 21:10
 */
public class NettySocketUtils {

    private static final SocketIOServer SOCKET_SERVER;
    static {
        SOCKET_SERVER = SpringContextHolder.getBean(SocketIOServer.class);
    }

    /**
     * 发送事件给当前用户
     */
    public static void sendEvent2Self(SocketIOClient client, String eventName, Object data) {
        client.sendEvent(eventName, data);
    }

    /** 发送事件给所有在线用户 */
    public static void sendEvent2All(String eventName, Object data) {
        SOCKET_SERVER.getBroadcastOperations().sendEvent(eventName, data);
    }

    /**
     * 发送事件给除了自己的在线用户
     */
    public static void sendEvent2AllExceptSelf(SocketIOClient client, String eventName, Object data) {
        SOCKET_SERVER.getBroadcastOperations().sendEvent(eventName, client, data);
    }

}
